package sd.czw.com.miemiegoushop;

import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import sd.czw.com.miemiegoushop.adapter.MyAdapter;
import sd.czw.com.miemiegoushop.base.CzwBaseActivity;
import sd.czw.com.miemiegoushop.data.Contants;
import sd.czw.com.miemiegoushop.data.GoodsModelk;
import sd.czw.com.miemiegoushop.ui.MyCallBak;
import sd.czw.com.miemiegoushop.utils.CzwNetWorkUtils;
import sd.czw.com.miemiegoushop.utils.loading.CzwLoadingView;

/**
 * demo  演示  封装过的activity状态管理 和  刷新加载更多的功能
 *
 * 通过butterknife注解  获取控件
 * ---CzwNetWorkUtils  获取手机网络状态
 * ---CzwLoadingView   切换页面状态
 * ---SpringView       模仿阿里的刷新和加载更多
 *
 */

public class MainActivity extends CzwBaseActivity {
    @Bind(R.id.toolbar_title)
    TextView toolbarTitle;
    @Bind(R.id.recycleview)
    RecyclerView recyclerView;
    @Bind(R.id.springview)
    SpringView springView;
    private List<GoodsModelk.ErrmsgBean> titles = new ArrayList<>();
    private MyAdapter myAdapter;
    private int start = 0;
    private int page = 0;
    private CzwLoadingView xLoadingView;
    private Map<String, String> params = new HashMap<>();

    private void loaddatas(Map<String, String> params) {
        OkGo.<GoodsModelk>post("http://miemiegou.com/wap/app_index_getgoods.php")
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .cacheTime(3600 * 1000)
                .params(params)
                .execute(new MyCallBak<GoodsModelk>(GoodsModelk.class, this) {

                    @Override
                    public void onSuccess(Response<GoodsModelk> response) {
                        if (response.body().getErrcode() == 1) {
                            titles.addAll(response.body().getErrmsg());
                            myAdapter.notifyDataSetChanged();
                            getLoading(3);
                        }
                    }

                    @Override
                    public void onError(Response<GoodsModelk> response) {
                        if (!CzwNetWorkUtils.isConnected(MainActivity.this)) {
                            getLoading(0);
                        } else {
                            getLoading(1);
                        }
                    }
                });
    }


    public void getLoading(int state) {
        switch (state) {
            case 0:
                //无网络
                xLoadingView.showNoNetwork();
                break;
            case 1:
                //接口回调错误
                xLoadingView.showError();
                break;
            case 3:
                //显示加载内容
                xLoadingView.showContent();
                break;
            case 4:
                //无数据
                xLoadingView.showEmpty();
                break;
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    public void initView() {
        int width = getWindowManager().getDefaultDisplay().getWidth();
        toolbarTitle.setText("activity状态管理、刷新加载更多");

        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(manager);
        myAdapter = new MyAdapter(this, width, titles);
        recyclerView.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();

        xLoadingView = CzwLoadingView.wrap(this);

        params.put("time", Contants.Time);
        params.put("sign", Contants.Sign);
        params.put("start", String.valueOf(start));
        params.put("limit", "20");
    }

    @Override
    public void initData() {
        loaddatas(params);
        if (titles == null) {
            xLoadingView.showEmpty();
        }
        xLoadingView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CzwNetWorkUtils.isConnected(MainActivity.this)) {
                    Toast.makeText(MainActivity.this, "无网络连接", Toast.LENGTH_SHORT).show();
                    if (titles.size() == 0) {
                        getLoading(0);
                    }
                } else {
                    start = 0;
                    titles.clear();
                    params.put("start", String.valueOf(start));
                    loaddatas(params);
                    if (titles.size() == 0) {
                        getLoading(4);
                    }
                    getLoading(3);
                }
            }
        });

        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!CzwNetWorkUtils.isConnected(MainActivity.this)) {
                            getLoading(0);
                        } else {
                            start = 0;
                            titles.clear();
                            params.put("start", String.valueOf(start));
                            loaddatas(params);
                            myAdapter.notifyDataSetChanged();
                            springView.onFinishFreshAndLoad();
                            if (titles.size() == 0) {
                                getLoading(4);
                            }
                            getLoading(3);
                        }
                    }
                }, 1000);
            }

            @Override
            public void onLoadmore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        start = page * 20;
                        params.put("start", String.valueOf(start));
                        loaddatas(params);
                        myAdapter.notifyDataSetChanged();
                        springView.onFinishFreshAndLoad();
                    }
                }, 1500);
            }
        });
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
    }
}
